package br.ufrn.findabomb.model;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

public class ServerResponse {
	enum Response {
		SUCCESS,
		FAIL
	}
	public enum ResponseType {
		PLAYER,
		ROOM,
		LOGIN,
		DATA_GAME,
		REASON, LIST_PLAYERS, LIST_ROOM, UNKNOWN
	}
	
	private Response res;
	private ResponseType type;
	private Object data;
	private boolean suc;
	
	public Response getRes() {
		return res;
	}

	public ResponseType getType() {
		return type;
	}

	public Object getData() {
		return data;
	}

	public void setSuc(boolean suc) {
		this.suc = suc;
	}

	public void setRes(Response res) {
		this.res = res;
	}

	public void setType(ResponseType type) {
		this.type = type;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public boolean isSuccessfull() {
		return suc;
	}

	public static ServerResponse parse(JSONObject str) throws JSONException {
		ServerResponse sr = new ServerResponse();
		sr.setSuc(str.getBoolean("success"));
		switch(str.getInt("type")) {
			case 1: 
				sr.setType(ResponseType.PLAYER);
				sr.setData((Player)str.get("data"));
				break;
			case 2: 
				sr.setType(ResponseType.ROOM);
				sr.setData((Room)str.get("data"));
				break;
			case 4:
				sr.setType(ResponseType.LIST_PLAYERS);
				sr.setData((List<Player>) str.get("data"));
				break;
			case 5: 
				sr.setType(ResponseType.LIST_ROOM); 
				sr.setData((List<Room>) str.get("data"));
				break;
			default: 
				sr.setType(ResponseType.UNKNOWN); 
				break;
		}
		return sr;
	}
}
